package com.gmy.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmy.commonutils.Result;
import com.gmy.eduservice.entity.EduTeacher;
import com.gmy.eduservice.entity.vo.TeacherQuery;
import com.gmy.eduservice.service.EduTeacherService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.temporal.Temporal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-27
 */

@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    public EduTeacherService teacherService;

    // 1.查询所以的数据  restful 风格
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public Result findAllTea(){
        List<EduTeacher> list = teacherService.list(null);
        return Result.ok().data("items",list);
    }


    //2.逻辑删除
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/delete/{id}")
    public Result removeById(@ApiParam(name = "id", value = "讲师ID", required = true)
                                  @PathVariable("id") String id){
        boolean b = teacherService.removeById(id);

        if(b){
            return Result.error();
        }
        else {
            return Result.ok();
        }

    }


    //3.分页查询
    @ApiOperation(value = "分页查询")
    @GetMapping("/pageTeacher/{cur}/{limit}")
    public Result pageListTea(@PathVariable("cur") Integer cur,
                              @PathVariable("limit") Integer limit){

        Page<EduTeacher> teacherPage = new Page<>(cur, limit);

        teacherService.page(teacherPage,null);

        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();

//        Map map =new HashMap<>();
//        map.put("total",total);
//        map.put("rows",records);
//
        return Result.ok().data("total",total).data("rows",records);

    }

    //4.多条件组合查询带分页
    @ApiOperation(value = "多条件组合查询带分页")
    @PostMapping("/pageTeacherCondition/{cur}/{limit}")
    public Result pageTeacherCondition(@PathVariable("cur") long cur,
                                       @PathVariable("limit") long limit,
                                       // 条件值 可以为空 ： 而且必须用post请求 ！
                                       @RequestBody(required = false) TeacherQuery teacherQuery){

         Page<EduTeacher> teacherPage = new Page<>(cur, limit);

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        final String name = teacherQuery.getName();
        final Integer level = teacherQuery.getLevel();
        final String begin = teacherQuery.getBegin();
        final String end = teacherQuery.getEnd();

        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }


        // 排序：
        wrapper.orderByDesc("gmt_modified");

        teacherService.page(teacherPage,wrapper);

        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();

        return Result.ok().data("total",total).data("rows",records);

    }


    //5.添加讲师：
    @ApiOperation(value = "添加讲师：")
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody EduTeacher eduTeacher){

        final boolean save = teacherService.save(eduTeacher);

        if(save){
            return Result.ok();
        }else {
            return Result.error();
        }

    }


    // 6.查询功能
    @GetMapping("/getTeacher/{id}")
    public Result getTeacher(@PathVariable ("id") String id){

        final EduTeacher t = teacherService.getById(id);

        return Result.ok().data("teacher",t);
    }

    //7.修改
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher){

        final boolean b = teacherService.updateById(eduTeacher);
        if(b){
            return Result.ok();
        }else {
            return Result.error();
        }

    }

}

